package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.PmsSkuImagesEntity;
import com.atguigu.gulimall.product.entity.PmsSpuInfoDescEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.ItemSaleAttrVo;
import com.atguigu.gulimall.product.vo.SeckillInfoVo;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroup;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.PmsSkuInfoDao;
import com.atguigu.gulimall.product.entity.PmsSkuInfoEntity;

import javax.annotation.Resource;


@Service("pmsSkuInfoService")
public class PmsSkuInfoServiceImpl extends ServiceImpl<PmsSkuInfoDao, PmsSkuInfoEntity> implements PmsSkuInfoService {

    @Autowired
    private PmsSkuImagesService imagesService;

    @Autowired
    private PmsSpuInfoDescService spuInfoDescService;

    @Autowired
    private PmsAttrGroupService attrGroupService;

    @Autowired
    private PmsSkuSaleAttrValueService skuSaleAttrValueService;

//    @Autowired
//    private SeckillFeignService seckillFeignService;


    @Resource
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * key: 2222
         * catelogId: 225
         * brandId: 2
         * min: 1
         * max: 2
         */
        QueryWrapper<PmsSkuInfoEntity> queryWrapper=new QueryWrapper<>();
        String catalogId= (String) params.get("catelogId");
        String brandId= (String) params.get("brandId");
        String key=(String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            queryWrapper.and(q->{
                q.eq("sku_name",key).or().like("sku_desc",key);
            });
        }
        if(StringUtils.isNotBlank(catalogId) && !"0".equals(catalogId)) queryWrapper.eq("catalog_id",catalogId);
        if(StringUtils.isNotBlank(brandId) && !"0".equals(brandId)) queryWrapper.eq("brand_id",brandId);
        if(params.get("min")!=null && !"0".equals(params.get("min").toString())){
            Integer min=Integer.parseInt(params.get("min").toString());
            queryWrapper.ge("price",min);
        }
        if(params.get("max")!=null && !"0".equals(params.get("max").toString())){
            Integer max=Integer.parseInt(params.get("max").toString());
            queryWrapper.le("price",max);
        }

        IPage<PmsSkuInfoEntity> page = this.page(
                new Query<PmsSkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }
    @Override
    public void saveSkuInfo(PmsSkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<PmsSkuInfoEntity> infoFutrue = CompletableFuture.supplyAsync(() -> {
            //1 sku基本信息
            PmsSkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> ImgageFuture = CompletableFuture.runAsync(() -> {
            //2 sku图片信息
            List<PmsSkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);

        CompletableFuture<Void> saleAttrFuture =infoFutrue.thenAcceptAsync(res -> {
            //3 获取spu销售属性组合
            List<ItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrsBuSpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        },executor);

        CompletableFuture<Void> descFuture = infoFutrue.thenAcceptAsync(res -> {
            //4 获取spu介绍
            PmsSpuInfoDescEntity spuInfo = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfo);
        },executor);

        CompletableFuture<Void> baseAttrFuture = infoFutrue.thenAcceptAsync(res -> {
            //5 获取spu规格参数信息
            List<SpuItemAttrGroup> attrGroups = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroups);
        }, executor);

        // 6.查询当前sku是否参与秒杀优惠
//        CompletableFuture<Void> secKillFuture = CompletableFuture.runAsync(() -> {
//            R skuSeckillInfo = seckillFeignService.getSkuSeckillInfo(skuId);
//            if (skuSeckillInfo.getCode() == 0) {
//                SeckillInfoVo seckillInfoVo = skuSeckillInfo.getData(new TypeReference<SeckillInfoVo>() {});
//                skuItemVo.setSeckillInfoVo(seckillInfoVo);
//            }
//        }, executor);

        // 等待所有任务都完成再返回
        CompletableFuture.allOf(ImgageFuture,saleAttrFuture,descFuture,baseAttrFuture).get();
        return skuItemVo;
    }

}